﻿using ConfExpert.Datasets;
using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms
{
    public partial class AdhocQuery : XForm
    {
        public enum AdHocQueries
        {
            Du4DepodakiToplar = 1,
            CikilmisToplar = 2,
            Du4EskiKesim = 3,
            Du4DosyaBazindaKesimAdetleri = 4,
            WebRezerveOnay = 5,
            Du4GomlekEtiketi = 6,
            KaufGomlekEtiketi = 7,
            ElshofGomlekEtiketi = 8
        }

        AdHocQueries Type;
        public AdhocQuery(AdHocQueries type)
        {
            InitializeComponent();
            this.Text = txtInfo.Caption = "Raporlar";
            Type = type;

            gvMaster.OptionsView.AllowCellMerge = true;
            gvMaster.CellMerge += new DevExpress.XtraGrid.Views.Grid.CellMergeEventHandler(gv_CellMerge);

            gvMaster.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(gvMaster_FocusedRowChanged);

            switch (type)
            {
                case AdHocQueries.Du4DepodakiToplar:
                    Du4DepodakiToplar();
                    break;
                case AdHocQueries.CikilmisToplar:
                    CikilanToplar();
                    break;
                case AdHocQueries.Du4EskiKesim:
                    Du4EskiKesim();
                    break;
                case AdHocQueries.Du4DosyaBazindaKesimAdetleri:
                    Du4DosyaBazindaKesimAdetleri();
                    break;
                case AdHocQueries.WebRezerveOnay:
                    //WebRezerveOnay();
                    break;
                case AdHocQueries.Du4GomlekEtiketi:
                    Du4GomlekEtiketi();
                    break;
                case AdHocQueries.KaufGomlekEtiketi:
                    KaufGomlekEtiketi();
                    break;
                case AdHocQueries.ElshofGomlekEtiketi:
                    ElshofGomlekEtiketi();
                    break;
            }

            foreach (GridColumn col in gvMaster.Columns)
                col.OptionsFilter.FilterPopupMode = FilterPopupMode.CheckedList;

            foreach (GridColumn col in gvDetail.Columns)
                col.OptionsFilter.FilterPopupMode = FilterPopupMode.CheckedList;

        }


        private void CikilanToplar()
        {
            this.Text = "Çıkılmış Toplar";
            this.lblHeader.Caption = string.Empty;  // "Sistemden daha önceden çıkılmış tüm topları listeler.";
            gvMaster.OptionsView.AllowCellMerge = false;
            gvMaster.CellMerge -= new CellMergeEventHandler(gv_CellMerge);

            gvMaster.Init("GetAdhocQuery", 2);
            gvMaster.AddDetailGrid(gvDetail, "ProductId");

            gvDetail.FormatAsDate("ModifiedOn");
            gvDetail.AddSummary("Qty");

            gvMaster.SetCaptions("UreticiKodu:Üretici Kodu", "MusteriKodu:Müşteri Kodu", "Qty:Toplam Metraj");
            gvDetail.SetCaptions("Qty:Metraj", "OrderRef:OrderRef.");
        }

        void gvMaster_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            gvDetail.BestFitColumns();
        }


        void SepeteEkle(object sender, EventArgs e)
        {
            string rollIds = "";
            foreach (int handle in gvDetail.GetSelectedRows())
            {
                rollIds += gvDetail.GetRowCellValue(handle, "RollId").ToString() + ",";
            }
            rollIds = rollIds.Remove(rollIds.Length - 1, 1);
            ExecuteNonQuery("UpdSepetByRollIds", rollIds, WhoAmI);

        }

        void SepettenCikar(object sender, EventArgs e)
        {
            string rollIds = "";
            foreach (int handle in gvDetail.GetSelectedRows())
            {
                rollIds += gvDetail.GetRowCellValue(handle, "RollId").ToString() + ",";
            }
            rollIds = rollIds.Remove(rollIds.Length - 1, 1);
            ExecuteNonQuery("UpdSepetByRollIdsRemove", rollIds, WhoAmI);
        }

        void gv_CellMerge(object sender, DevExpress.XtraGrid.Views.Grid.CellMergeEventArgs e)
        {
            GridView gv = (GridView)sender;
            if (Type == AdHocQueries.Du4EskiKesim ||
                Type == AdHocQueries.Du4DepodakiToplar)
            {
                e.Merge = gv.GetRowCellDisplayText(e.RowHandle1, e.Column) == gv.GetRowCellDisplayText(e.RowHandle2, e.Column) &&
                    gv.GetRowCellDisplayText(e.RowHandle1, "ProductId") == gv.GetRowCellDisplayText(e.RowHandle2, "ProductId") &&
                    !e.Column.FieldName.Contains("Metraj") &&
                    !e.Column.FieldName.Contains("Qty");

                e.Handled = true;
            }
        }

        private void Du4EskiKesim()
        {
            lblHeader.Caption = "Du4 Eski Kesim";
            gvMaster.AddSummary("Qty");

            splitter.Panel1.Dock = DockStyle.Fill;
            splitter.Panel2.Visible = false;

            gvMaster.Init("GetAdhocQuery", 3);
        }

        private void Du4GomlekEtiketi()
        {
            lblHeader.Caption = "Du4 Gömlek Etiketleri ";
            gvMaster.AddSummary();

            splitter.Panel1.Dock = DockStyle.Fill;
            splitter.Panel2.Visible = false;

            gvMaster.Init("GetAdhocQuery", 6);

        }



        private void KaufGomlekEtiketi()
        {
            lblHeader.Caption = "Kauf Gömlek Etiketleri ";
            gvMaster.AddSummary("Qty");
            gvMaster.OptionsView.AllowCellMerge = false;
            gvMaster.CellMerge -= new DevExpress.XtraGrid.Views.Grid.CellMergeEventHandler(gv_CellMerge);

            splitter.Panel1.Dock = DockStyle.Fill;
            splitter.Panel2.Visible = false;

            gvMaster.Init("GetAdhocQuery", 7);

            //gvMaster.RowCellStyle += gv_RowCellStyle;
            gvMaster.ColumnFilterChanged += gvMaster_ColumnFilterChanged;
            gvMaster.BestFitColumns();
        }

        private void ElshofGomlekEtiketi()
        {
            lblHeader.Caption = "Elshof Gömlek Etiketleri ";
            gvMaster.AddSummary("Qty");

            splitter.Panel1.Dock = DockStyle.Fill;
            splitter.Panel2.Visible = false;

            gvMaster.Init("GetAdhocQuery", 8);
            //gvMaster.RowCellStyle += gv_RowCellStyle;
            gvMaster.BestFitColumns();
        }

        void gvMaster_ColumnFilterChanged(object sender, EventArgs e)
        {
            int dagilimId = -1;
            bool multi = false;
            for (int i = 0; i < gvMaster.DataRowCount; i++)
            {
                int rowHandle = gvMaster.GetVisibleRowHandle(i);
                System.Data.DataRowView dr = (System.Data.DataRowView)gvMaster.GetRow(rowHandle);
                int rowDagilimId = (int)dr["DagilimId"];

                if (dagilimId == -1)
                    dagilimId = rowDagilimId;

                if (dagilimId != -1 && rowDagilimId != dagilimId)
                {
                    multi = true;
                    break;
                }
            }
            if (!multi)
            {
                MyData.DagilimRow r = AppSession.Lookups.Dagilim.FindByDagilimId(dagilimId);
                if (r != null)
                    for (int j = 1; j <= 28; j++)
                    {
                        string fieldName = String.Format("A{0:00}", j);
                        string cap = r[String.Format("A{0:00}", j)].ToString();
                        if (cap == "")
                            gvMaster.Columns[fieldName].Visible = false;

                        else
                            gvMaster.Columns[fieldName].Caption = cap;
                    }
                gvMaster.BestFitColumns();
            }
            else
                for (int j = 1; j <= 28; j++)
                {
                    string fieldName = String.Format("A{0:00}", j);
                    gvMaster.Columns[fieldName].Caption = fieldName;
                    gvMaster.Columns[fieldName].VisibleIndex = 100 + j;

                }

        }




        private void Du4DepodakiToplar()
        {
            lblHeader.Caption = "DU4 | Depodaki Toplar";

            gvMaster.Init("GetAdhocQuery", 1);

            gvMaster.AddSummary();
            gvMaster.AddSummary("Qty");

            splitter.Panel1.Dock = DockStyle.Fill;
            splitter.Panel2.Visible = false;

            gvMaster.OptionsBehavior.ReadOnly = false;
            //gv.OptionsBehavior.Editable = true;

            //foreach (GridColumn col in gv.Columns)
            //{
            //    col.OptionsColumn.ReadOnly = true;
            //    col.OptionsColumn.AllowEdit = false;
            //}

            //gv.Columns["Selection"].OptionsColumn.ReadOnly = false;
            //gv.Columns["Selection"].OptionsColumn.AllowEdit = true;
            gvMaster.OptionsBehavior.EditorShowMode = EditorShowMode.MouseDownFocused;
        }

        private void Du4DosyaBazindaKesimAdetleri()
        {
            gvMaster.OptionsView.AllowCellMerge = false;
            gvMaster.CellMerge -= new DevExpress.XtraGrid.Views.Grid.CellMergeEventHandler(gv_CellMerge);

            lblHeader.Caption = "Dosya Bazında Kesim Adetleri";

            gvMaster.Init("GetAdhocQuery", 4);

            gvMaster.SetCaptions("SonGuncelleme:Son Güncelleme", "Siparis:Siparişteki Miktar", "Kesilen:Kesilen Miktar");


            foreach (GridColumn col in gvMaster.Columns)
                col.OptionsFilter.FilterPopupMode = FilterPopupMode.CheckedList;

            gvMaster.AddSummary("", "Siparis", "Kesilen", "Fark");


            splitter.Panel1.Dock = DockStyle.Fill;
            splitter.Panel2.Visible = false;

        }


        private void AdhocQuery_Load(object sender, System.EventArgs e)
        {
            splitter.SplitterPosition = splitter.Width / 2;

        }


        int DagilimId = 0;
        private void gv_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                if (e.Column.FieldName.Length == 3 &&
                    e.Column.FieldName.StartsWith("A"))
                {
                    DataRow dr = gvMaster.GetDataRow(e.RowHandle);
                    int D = intParse(dr["DagilimId"]);

                    e.Appearance.ForeColor = (DagilimId != D ? Color.LightSlateGray : Color.Black);
                }
            }
        }

    }
}